package com.lagou.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MaxAgeCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、创建cookie对象，设置数据
        Cookie cookie = new Cookie("name", "jack");
        // 设置cookie的存活时间
        // -1 浏览器关闭，cookie就销毁
        // cookie.setMaxAge(-1);
        // 正数，存活60秒，过期销毁
        cookie.setMaxAge(60);
        // 0 立即销毁
        cookie.setMaxAge(0);

        // 2、通过response对象响应cookie
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
